import Vue from 'vue';
import Vuex from 'vuex';
import state from '@/store/states';
import crypto from '@/assets/js/crypto';
import getters from '@/store/getters';
import mutations from '@/store/mutations';
import actions from '@/store/actions';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions,
  plugins: [createPersistedState({
    // key: window.atob('highwayScrect'),
    reducer: (allStates) => ({
      token: allStates.token,
      userInfo: allStates.userInfo,
      naviItems: allStates.naviItems,
      selectedKeys: allStates.selectedKeys,
      openKeys: allStates.openKeys,
      openPages: allStates.openPages,
      btnsPermissionList: allStates.btnsPermissionList,
    }),
    storage: {
      getItem: (key) => (window.sessionStorage.getItem(key) ? crypto.decrypt(window.sessionStorage.getItem(key)) : ''),
      setItem: (key, value) => window.sessionStorage.setItem(key, crypto.encrypt(value)),
      removeItem: (key) => window.sessionStorage.removeItem(key),
    },
  })],
});
